package com.wngbms.websocket.zookeeper;

import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;

@Slf4j
@Configuration
public class ZookeeperConfig implements ZkConst {

    @Bean
    public CuratorFramework curatorFramework() {
        return CuratorFrameworkFactory.builder()
                .connectString("localhost:2181")
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .connectionTimeoutMs(5000)
                .sessionTimeoutMs(5000)
                .build();
    }

    @Bean
    public ApplicationRunner curatorStartTask(CuratorFramework curatorFramework,
                                              ZookeeperSupport zookeeperSupport,
                                              ExecutorService zookeeperThreadPool) {
        return new CuratorStartRunner(curatorFramework,
                zookeeperSupport, zookeeperThreadPool);
    }

}
